﻿using DevComponents.DotNetBar;
using RFID.PC.AppCenter;
using RFID.PC.CardCenter;
using System;
using System.IO.Ports;

namespace RFID.PC
{
    public partial class MainForm : Office2007RibbonForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //获取当前下拉框
            string[] ports = SerialPort.GetPortNames();
            //把获得的数组加载到下拉框中
            cmbCom.Items.AddRange(ports);
        }

        private void MainForm_MdiChildActivate(object sender, EventArgs e)
        {
            foreach (var form in MdiChildren)
            {
                if (form != ActiveMdiChild)
                    form.Close();
            }
            if (ActiveMdiChild != null)
                toolStripStatusLabel1.Text = "当前活动窗口：" + ActiveMdiChild.Text;
            else
                toolStripStatusLabel1.Text = "";
        }

        /// <summary>
        /// 当前打开的串口
        /// </summary>
        public IntPtr Com { get; private set; }

        /// <summary>
        /// 检测串口是否可用
        /// </summary>
        public bool ComEnable => Com != IntPtr.Zero && (int)Com != -1;

        private void BtnOpenCom_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(cmbCom.ControlText))
            {
                toolStripStatusLabel1.Text = "请先选择串口!";
            }
            else
            {
                int port = Convert.ToInt32(cmbCom.ControlText.Replace("COM", ""));
                Com = EPCSDKHelper.OpenComm(port);
                if (ComEnable)
                {
                    toolStripStatusLabel1.Text = "";
                    tslCom.Text = cmbCom.ControlText;
                }
            }
        }

        private void BtnCloseCom_Click(object sender, EventArgs e)
        {
            if (!ComEnable)
            {
                toolStripStatusLabel1.Text = "未识别到已打开串口!";
            }
            else
            {
                EPCSDKHelper.CloseComm(Com);
                Com = IntPtr.Zero;
                tslCom.Text = "    ";
            }
        }

        private void BtnAddCard_Click(object sender, EventArgs e)
        {
            new AddCardForm(this, sender).Show();
        }

        private void BtnSearchCard_Click(object sender, EventArgs e)
        {
            new SearchCardForm(this, sender).Show();
        }

        private void BtnAddRich_Click(object sender, EventArgs e)
        {
            new AddRichForm(this, sender).Show();
        }

        private void BtnListRich_Click(object sender, EventArgs e)
        {
            new ListRichForm(this, sender).Show();
        }

        private void BtnChangePwd_Click(object sender, EventArgs e)
        {
            new ChangePwdForm(this, sender).Show();
        }

        private void btnLostPwd_Click(object sender, EventArgs e)
        {
            new LostPwdForm(this, sender).Show();
        }

        private void BtnAddWork_Click(object sender, EventArgs e)
        {
            new AddWorkForm(this, sender).Show();
        }

        private void BtnListWork_Click(object sender, EventArgs e)
        {
            new ListWorkForm(this, sender).Show();
        }

        private void BtnChangeCard_Click(object sender, EventArgs e)
        {
            new ChangeCardForm(this, sender).Show();
        }

        private void BtnActiviteCard_Click(object sender, EventArgs e)
        {
            new ActiveCardForm(this, sender).Show();
        }

        private void BtnLostCard_Click(object sender, EventArgs e)
        {
            new LostCardForm(this, sender).Show();
        }

        private void BtnDistroyCard_Click(object sender, EventArgs e)
        {
            new DestoryCardForm(this, sender).Show();
        }

        private void BtnAddOrder_Click(object sender, EventArgs e)
        {
            new AddOrderForm(this, sender).Show();
        }

        private void BtnListOrder_Click(object sender, EventArgs e)
        {
            new ListOrderForm(this, sender).Show();
        }
    }
}
